package com.yeyks.commonReference.service.consumer.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.base.PageInfo;
import com.yeyks.common.constants.ConfigKey;
import com.yeyks.common.em.MessageNoticeType;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.commonReference.service.config.ConfigService;
import com.yeyks.commonReference.service.consumer.MessageNoticeTemplateService;
import com.yeyks.consumer.dal.dao.MessageNoticeMapper;
import com.yeyks.consumer.dal.domain.MessageNotice;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.dto.MessageNoticeDTO;
import com.yeyks.consumer.service.MessageNoticeService;
import com.yeyks.other.dal.dao.ActivityInfoMapper;
import com.yeyks.other.dal.domain.ActivityInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Slf4j
@Service
public class MessageNoticeServiceImpl extends ServiceImpl<MessageNoticeMapper, MessageNotice> implements MessageNoticeService {

    @Autowired
    private MessageNoticeTemplateService messageNoticeTemplateService;
    @Autowired
    private ActivityInfoMapper activityInfoMapper;
    @Autowired
    private ConfigService configService;

    private void saveMessageNotice(String template, Map<String, Object> param, Integer userId, Integer type, Integer hotelId, Integer activityId) {
        Map<String, String> map = messageNoticeTemplateService.commonValue(param, template);
        MessageNotice messageNotice = new MessageNotice();
        messageNotice.setUserId(userId);
        messageNotice.setHotelId(hotelId);
        messageNotice.setActivityId(activityId);
        messageNotice.setType(type);
        messageNotice.setTitle(map.get("title"));
        messageNotice.setContent(map.get("content"));
        messageNotice.setIconUrl(map.get("iconUrl"));
        baseMapper.insert(messageNotice);
    }

    @Override
    public void saveMessageNoticeSimple(String template, Map<String, Object> param, Integer userId, Integer type) {
        saveMessageNotice(template, param, userId, type, null, null);
    }

    @Override
    public MessageNotice getMessageNotice(Map<String, String> map, Integer userId, Integer hotelId) {
        MessageNotice messageNotice = new MessageNotice();
        messageNotice.setContent(map.get("content"));
        messageNotice.setHotelId(hotelId);
        messageNotice.setIconUrl(map.get("iconUrl"));
        messageNotice.setTitle(map.get("title"));
        messageNotice.setUserId(userId);
        messageNotice.setType(MessageNotice.Type.order.value());
        return messageNotice;
    }

    @Override
    public int createNoticeAndSave(Map<String, String> map, Integer userId, Integer hotelId) {
        MessageNotice messageNotice = getMessageNotice(map, userId, hotelId);
        return baseMapper.insert(messageNotice);
    }

    private MessageNotice activityMessageNotice(Map<String, String> map, Integer userId, Integer type) {
        MessageNotice messageNotice = new MessageNotice();
        messageNotice.setContent(map.get("content"));
        messageNotice.setIconUrl(map.get("iconUrl"));
        messageNotice.setTitle(map.get("title"));
        messageNotice.setUserId(userId);
        messageNotice.setType(type);
        messageNotice.setCreatedTime(new Date());
        return messageNotice;
    }

    @Override
    public int activieyCreateAndSave(Map<String, String> map, Integer userId, Integer type) {
        MessageNotice messageNotice = activityMessageNotice(map, userId, type);
        return baseMapper.insert(messageNotice);
    }

    @Override
    public int activieyIdCreateAndSave(Map<String, String> map, Integer activityId, Integer type) {
        MessageNotice messageNotice = activityMessageNotice(map, null, type);
        messageNotice.setActivityId(activityId);
        return baseMapper.insert(messageNotice);
    }

    /**
     * 分页获取通知消息
     *
     * @param userInfo
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> list(UserInfo userInfo, Integer type, Integer pageNo, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageInfo pageInfo = PageInfo.of(pageNo, pageSize);
            Map<String, Object> param = new HashMap<>();
            param.put("userId", userInfo.getId());
            param.put("type", type);
            param.put("offset", pageInfo.getOffset());
            param.put("limit", pageInfo.getLimit());
            List<MessageNotice> lists = baseMapper.list(param);
            if (lists == null) {
                return map;
            }
            List<MessageNoticeDTO> messageNoticeDTOS = getMessageNoticeDTOS(lists);
            map.put("list", messageNoticeDTOS);
            Integer count = baseMapper.listCount(param);
            map.put("count", count);
        } catch (Exception e) {
            log.error("分页获取通知消息失败{}", e.getLocalizedMessage());
            e.printStackTrace();
            return map;
        }
        return map;
    }

    private List<MessageNoticeDTO> getMessageNoticeDTOS(List<MessageNotice> lists) {
        List<MessageNoticeDTO> messageNoticeDTOS = new ArrayList<>();
        for (MessageNotice messageNotice : lists) {
            MessageNoticeDTO messageNoticeDTO = ObjectConvertUtil.convertObject(messageNotice, MessageNoticeDTO.class);
            if (messageNotice.getType() != null
                    && messageNotice.getType().equals(MessageNoticeType.activity_activity.value())) {
                Integer activityId = messageNotice.getActivityId();
                if (activityId != null) {
                    ActivityInfo activityInfo = activityInfoMapper.selectById(Long.valueOf(activityId));
                    if (activityInfo != null) {
                        messageNoticeDTO.setUrl(getActivityUrl(activityInfo));
                    }
                }
            }
            messageNoticeDTOS.add(messageNoticeDTO);
        }
        return messageNoticeDTOS;
    }

    private String getActivityUrl(ActivityInfo activityInfo) {
        try {
            if (activityInfo.getType() != null && activityInfo.getType() == 1) {
                return activityInfo.getUrlOrText();
            } else {
                String value = configService.getValue(ConfigKey.ACTIVITY_URL);
                if (value != null) {
                    return value + activityInfo.getId();
                }
                return "https://kuaisutest.chengguokj.com/h5";
            }
        } catch (Exception e) {
            log.error("个推发送消息获取链接异常{}", e.getLocalizedMessage());
            e.printStackTrace();
            return "https://kuaisutest.chengguokj.com/h5";
        }
    }

    /**
     * 分页获取通知消息
     *
     * @param param
     * @return
     */
    @Override
    public List<MessageNotice> detailList(Map<String, Object> param) {
        return baseMapper.detailList(param);
    }


}
